﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SAD.Models;

namespace SAD.Controllers
{
    public class FoodController : Controller
    {
        //
        // GET: /Food/


        DataClasses1DataContext data = new DataClasses1DataContext();

        /// <summary>
        /// default view
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var food = (from o in data.Foods select o).ToList();
            return View(food);
        }


        /// <summary>
        /// create new food (for admin)
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(Food food)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    data.Foods.InsertOnSubmit(food);
                    data.SubmitChanges();
                    return RedirectToAction("Index");
                }
                return View(food);
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
            
        }


        /// <summary>
        /// update food for admin
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Update(int id)
        {
            //load trang edit
            Food food = data.Foods.Single(x => x.foodID == id);
            return View(food);
        }

        [HttpPost]
        public ActionResult Update(Food food)
        {
            try
            {
                var updateFood = data.Foods.Single(x => x.foodID == food.foodID);
                updateFood.name = food.name;
                updateFood.price = food.price;
                updateFood.image = food.image;
                UpdateModel(updateFood);
                data.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
            
        }


        /// <summary>
        /// Delete food for admin
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            Food food = data.Foods.Single(x => x.foodID == id);
            return View(food);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirm(int id)
        {
            try
            {
                Food food = data.Foods.Single(x => x.foodID == id);
                data.Foods.DeleteOnSubmit(food);
                data.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                throw ex;
            }  
        }

        /// <summary>
        /// order food
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Order(int id)
        {
            Food food = data.Foods.Single(x => x.foodID == id);
            return View(food);
        }
        [HttpPost, ActionName("Order")]
        public ActionResult OrderConfirm(int id)
        {
            string accID = "";

            //get role
            if (Session["AccID"] != null)
            {
                accID = Session["AccID"].ToString();
            }

            Food food = data.Foods.Single(x => x.foodID == id);
            //Account account = data.Accounts.Single(x => x.accountID == accID);
            //insert vào bảng order 
            Order order = new Order();
            order.accountID =int.Parse(accID);
            order.payment = food.price;
            order.date = System.DateTime.Today;
            data.Orders.InsertOnSubmit(order);
            data.SubmitChanges();

            //get record mới nhất trong bảng Order
            var order1 = (from o in data.Orders
                          orderby o.orderID descending
                          select o).First();

            Cart cart = new Cart();
            //insert vao bảng cart
            cart.orderID = order1.orderID;
            cart.foodID = food.foodID;
            cart.quantity = 1;
            data.Carts.InsertOnSubmit(cart);
            data.SubmitChanges();

            return RedirectToAction ("Index");
        }

        
    }
}
